<template>
  <el-dialog :model-value="Detail" width="40%" :before-close="handleClose">
    <template #header>
      <h3 style="padding-bottom: 10px">供应链详情</h3>
    </template>
    <el-form :model="form" label-width="auto">
      <el-form-item label="供应链名称：">
        <el-input v-model="form.supplyChainName" disabled />
      </el-form-item>
      <el-form-item label="供应链类型：">
        <el-input v-model="currentTypeName" disabled />
      </el-form-item>
      <el-form-item label="内容：">
        <WangEditor
          v-model="form.supplyChainInfo"
          height="200"
          :mode="'simple'"
          :defaultConfig="{
            readOnly: true,
            placeholder: '',
            autoFocus: false,
            MENU_CONF: {},
          }"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleClose">关闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup name="DetailsExpertDialog">
import { ref, watch } from 'vue';
import { getSupplyListSelect } from '@/api/Activity/supply.js';
// 表单数据
const form = ref({
  supplyChainName: '',
  typeId: '',
  supplyChainInfo: '',
});

const currentTypeName = ref('');
const loading = ref(false);

// 定义 props，接收父组件传递的数据
const props = defineProps({
  Detail: {
    type: Boolean,
  },
  DetailsRow: {
    type: Object,
  },
});

// 监听弹窗显示和数据变化
watch(
  () => props.Detail,
  (newVal) => {
    if (newVal && props.DetailsRow) {
      // 当弹窗打开且有数据时，初始化表单
      initFormData();
    }
  }
);

// 初始化表单数据
const initFormData = async () => {
  const row = props.DetailsRow;
  if (row.supplyChainName) {
    loading.value = true;
    try {
      const res = await getSupplyListSelect({});
      if (res.data.code === 200) {
        // 设置表单数据
        form.value = {
          supplyChainName: row.supplyChainName || '',
          typeId: row.typeId || '',
          supplyChainInfo: row.supplyChainInfo || '',
        };
        // 查找并设置活动类型名称
        if (row.typeId) {
          const currentType = res.data.data.find(
            (item) => String(item.id) === String(row.typeId)
          );
          currentTypeName.value = currentType ? currentType.name : '';
        }
      }
    } catch (error) {
      console.error('获取活动类型列表失败:', error);
    } finally {
      loading.value = false;
    }
  }
};

// 定义 emits，向父组件发送事件
const emit = defineEmits(['cancel']);

// 关闭对话框
const handleClose = () => {
  emit('cancel');
};
</script>

<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}

:deep(.w-e-text-container) {
  pointer-events: none !important;

  [data-w-e-textarea='true'] {
    cursor: default !important;
  }
}
</style>
